#include "ListNode.h"
unsigned listLength(ListNode *head) {
  unsigned count = 0;
  while (head) {
    count += 1;
    head = head->next;
  }
  return count;
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  unsigned minSize = listLength(headA);
  unsigned maxSize = listLength(headB);
  ListNode *result = nullptr;
  if (maxSize < minSize) {
    for (int i = 0; i < (minSize - maxSize); i++) {
      headA = headA->next;
    }
  } else {
    for (int i = 0; i < (maxSize - minSize); i++) {
      headB = headB->next;
    }
  }
  while (headA && headB) {
    if (headA == headB) {
      result = headA;
      break;
    } else {
      headA = headA->next;
      headB = headB->next;
    }
  }
  return result;
}